package com.dk.service.impl;

import com.dk.pojo.CarMessage;
import com.dk.mapper.CarMessageMapper;
import com.dk.service.CarMessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 帅杰
 * @since 2021-08-12
 */
@Service
public class CarMessageServiceImpl extends ServiceImpl<CarMessageMapper, CarMessage> implements CarMessageService {

    @Autowired
    private CarMessageMapper carMessageMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<CarMessage> toSelect() {
        String key="Hot";
        ZSetOperations zset= redisTemplate.opsForZSet();
        Long size = zset.size(key);//获取set集合的大小
        List<CarMessage> list = new ArrayList<>();
        //根据ZSet倒序每次自增1 分数+1
        redisTemplate.opsForZSet().add(key,1,1);
        //然后在通过ZSet将他们展示出来 l是起始数 l1是结尾数
        Set<ZSetOperations.TypedTuple<Integer>> set = zset.reverseRangeWithScores(key,0,-1);
        for (ZSetOperations.TypedTuple<Integer> s : set) {
            Long l = new Long(s.getValue());
            CarMessage t = carMessageMapper.selectById(l);
            if(t != null){
                list.add(t);
            }

        }
        return list;
    }
}
